<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        // 基于 Promise发送Ajax请求
        function queryData(url){
            var p = new Promise(function(resolve,reject){
                var xhr = new XMLHttpRequest()
                xhr.onreadystatechange = function(){
                    if(xhr.readyState != 4) return;
                    if(xhr.readyState == 4 && xhr.status == 200) {
                        resolve(xhr.responseText)
                    }else{
                        reject('服务器错误')
                    }
                }
                xhr.open('get',url)
                xhr.send(null)
            })
            return p
        }
        // queryData('http://localhost:3000/data1')
        //     .then(function(res){
        //         console.log(res)
        //     },function(err){
        //         console.log(err)
        //     })
        // ********************************************************
        // 发送多个Ajax请求并且保证顺序
        queryData('http://localhost:3000/data1')
            .then(function(res){
                console.log(res)
                return queryData('http://localhost:3000/data2')
            })
            .then(function(res){
                console.log(res)
                return queryData('http://localhost:3000/data3')
            })
            .then(function(res){
                console.log(res)
            })
        //通过 .then()的方式线性链式操作的解决回调地狱的嵌套，保证了请求的顺序
    </script>
</body>
</html>